﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniCT.LinkShareWpf.Interface;
using System.IO;
using System.Collections.Specialized;
using System.Xml.Serialization;


namespace UniCT.LinkShareWpf.CommunicationChannel
{
    [Serializable]
    [XmlRoot("MyResources")]
    public class MyResources
    {

        private HashSet<Resource> _resources;
        // Fortemente tipizzato di suo con le stringhe
        private NameValueCollection _bind;

        #region Static
        private static MyResources _instance = null;

        public static MyResources Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new MyResources();
                return _instance;
            }
        }
        #endregion

        public MyResources()
        {
            _resources = new HashSet<Resource>();
            _bind = new NameValueCollection();
            
        }



        [XmlArray("Resources")]
        [XmlArrayItem("Resource")]
        public HashSet<Resource> Resources
        {
            get { return _resources; }

        }
        [XmlIgnore]
        public NameValueCollection Bind
        {
            get { return _bind; }

        }


        public HashSet<String> getTitleList()
        {

           HashSet<String> hset = new HashSet<String>();
           foreach (Resource res in Resources)
               hset.Add(res.CInfo.Title);
           return hset;
        }

        public MyResources Read()
        {
            MyResources ret = null;
            XmlSerializer xs = null;
            try
            {

                xs = new XmlSerializer(typeof(MyResources));
            }
            catch
            {
                Console.WriteLine("read 1");
            }
            try
            {
                using (StreamReader sr = new StreamReader(@"..\..\test.xml"))
                {
                    ret = xs.Deserialize(sr) as MyResources;
                  
                    
                }
            }
            // Se il file non esiste vuol dire che è la prima esecuzione
            // e si crea un nuovo kad
            catch
            {

                Console.WriteLine("read 2");
            }



            return ret;

        }

        //Da mettere dentro il Read
        public void setBinding()
        {
           
            foreach (Resource res in this.Resources)
            {
             
                if (res.CInfo.GetType() == typeof(ContentInfoTVS))
                {
                    ContentInfoTVS tvSer = res.CInfo as ContentInfoTVS;
                    Bind.Add(tvSer.SeasonNumber.ToString(), tvSer.EpisodNumber.ToString());
                }
            }
            foreach(string sessKey in this.Bind.AllKeys)
                Bind.Add(sessKey, "All");
            Bind.Add("All", "All");
        }


        public void Save()
        {
            XmlSerializer ser = new XmlSerializer(typeof(MyResources));
            using (TextWriter writer = new StreamWriter(@"..\..\test.xml"))
            {
                ser.Serialize(writer, this);
            }
        }

    }
}
